package structure

import "sync"

// 可重置对象
type Resettable interface {
	Reset()
}

// 可重置对象结构缓存池
type CachePool[T Resettable] struct {
	Pool *sync.Pool
}

func (pool *CachePool[T]) Get() T {
	return pool.Pool.Get().(T)
}

func (pool *CachePool[T]) Put(obj T) {
	obj.Reset()
	pool.Pool.Put(obj)
}

// 新建缓存池
func NewCachePool[T Resettable](f func() any) (pool *CachePool[T]) {
	pool = new(CachePool[T])
	pool.Pool = &sync.Pool{
		New: f,
	}
	return
}
